package cn.edu.nuc.bre.controller;

import java.util.List;
import java.util.Map;

import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.identity.UserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

import cn.edu.nuc.bre.dto.UserInfo;
import cn.edu.nuc.bre.service.interfaces.GroupService;
import cn.edu.nuc.bre.service.interfaces.UserService;
import cn.edu.nuc.bre.util.PageInfo;

/**
 * 用户权限修改
 * 
 * @author Mine
 * @since 2016年11月11日_上午11:30:17
 */
@Controller
@Scope("prototype") // 原型
public class UserShipAction extends ActionSupport {
	/**
	 * 序列化id
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private UserService userService;
	@Autowired
	private GroupService groupService;
	@Autowired
	private IdentityService identityService;

	// 修改权限时的参数
	private String uid; // 用户
	private String gid;// 角色id
	private String noticeOfficer;// 判断是否是公告员

	private String tip;// 回显小消息

	private Map<String, List<Group>> groupsMap = null; // 分类的角色信息
	private PageInfo<UserInfo> pageInfo = null;
	// 分页信息
	private int targetPage;
	// 查询key
	private String keyword;

	// 获取用户Info列表
	@Override
	public String execute() throws Exception {
		// 设置查询条件
		UserQuery uq = identityService.createUserQuery();
		List<User> users = null;
		if (keyword != null && !"".equals(keyword.trim())) {
			uq = uq.userFirstNameLike("%" + keyword.trim() + "%");
		}
		long total = uq.count();// 获取总记录
		if (targetPage > 0) {
			users = uq.listPage((targetPage - 1) * 3, 3);// 默认页面大小
		} else {
			targetPage = 1;
			users = uq.listPage(0, 3);// 默认页面大小
		}
		// 转换为带权限用户
		List<UserInfo> uis = userService.findUserInfosByUsers(users);
		// 获取pageInfo
		pageInfo = new PageInfo<UserInfo>((int) total, targetPage, 3, uis);
		// 取得所有角色信息,并分类用于修改权限时显示
		groupsMap = groupService.findGroupsMapByTypes("1", "2", "3", "4", "11", "111");
		if (this.getTip() == null) {
			String temp = "";
			if (keyword != null && !"".equals(keyword.trim())) {
				temp = "关键字[" + this.getKeyword() + "]";
			}
			boolean flag = pageInfo.hasPageListErr();
			if (flag && this.getTip() == null) {
				tip = temp + "查询为空";
			} else {
				tip = temp + "查询成功";
			}
		}
		return SUCCESS;
	}

	/**
	 * 修改用户权限
	 */
	public String edit() throws Exception {
		boolean isNoticeOfficer = false;
		if (this.getNoticeOfficer() != null) {
			isNoticeOfficer = true;
		}
		if (userService.editGroupShip(this.getUid(), this.getGid(), isNoticeOfficer)) {
			// 修改成功
			this.setTip("修改" + this.uid + "号用户[用户组]成功。。。");
			return SUCCESS;
		} else {
			// 修改失败
			this.setTip("修改" + this.uid + "号用户[用户组]失败。。。");
			return ERROR;
		}
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public PageInfo<UserInfo> getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo<UserInfo> pageInfo) {
		this.pageInfo = pageInfo;
	}

	public int getTargetPage() {
		return targetPage;
	}

	public void setTargetPage(int targetPage) {
		this.targetPage = targetPage;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getGid() {
		return gid;
	}

	public void setGid(String gid) {
		this.gid = gid;
	}

	public String getNoticeOfficer() {
		return noticeOfficer;
	}

	public void setNoticeOfficer(String noticeOfficer) {
		this.noticeOfficer = noticeOfficer;
	}

	public Map<String, List<Group>> getGroupsMap() {
		return groupsMap;
	}

	public void setGroupsMap(Map<String, List<Group>> groupsMap) {
		this.groupsMap = groupsMap;
	}

}
